package com.cw.jeeyt.dao.impl.mdata;

import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

import com.cw.jeeyt.dao.intf.mdata.MdataAreaDao;
import com.cw.jeeyt.dao.mapper.mdata.MdataAreaMapper;
import com.cw.jeeyt.dao.model.mdata.MdataAreaDo;
import com.cw.jeeyt.dao.model.mdata.MdataAreaExample;
import com.cw.jeeyt.dao.model.mdata.MdataAreaKeyDo;
import com.cw.jeeyt.dao.query.mdata.MdataAreaQuery;
import com.cw.lang.common.dao.AbstractBaseDaoImpl;
import com.cw.lang.common.pagehelper.PageHelper;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.mybatis.dto.PageParam;
import com.cw.lang.common.utils.StringUtil;

/**
 * 地区数据库操作接口实现
 *
 * @author admin
 * @date 2021-03-26 22:30:39
 */
@Repository
public class MdataAreaDaoImpl extends AbstractBaseDaoImpl<MdataAreaDo, MdataAreaMapper> implements MdataAreaDao {
    /**
     * 分页查询
     *
     * @param mdataArea 条件
     * @param pageParam 分页参数
     * @return
     */
    @Override
    public PageInfo<MdataAreaDo> selectPage(MdataAreaQuery mdataArea, PageParam pageParam) {
        MdataAreaExample example = buildExample(mdataArea);
        return PageHelper.<MdataAreaDo>startPage(pageParam.getPageNum(), pageParam.getPageSize()).doSelectPageInfo(
            () -> getMapper().selectByExample(example));
    }

    /**
     * 根据key查询地区
     *
     * @param areaId
     * @return
     */
    @Override
    public MdataAreaDo selectByKey(String areaId) {
        MdataAreaKeyDo mdataAreaKeyDo = new MdataAreaKeyDo();
        mdataAreaKeyDo.setAreaId(areaId);
        return getMapper().selectByPrimaryKey(mdataAreaKeyDo);
    }

    /**
     * 根据key删除地区
     *
     * @param areaId
     * @return
     */
    @Override
    public int deleteByKey(String areaId) {
        MdataAreaKeyDo mdataAreaKeyDo = new MdataAreaKeyDo();
        mdataAreaKeyDo.setAreaId(areaId);
        return getMapper().deleteByPrimaryKey(mdataAreaKeyDo);
    }

    /**
     * 查询地区信息
     *
     * @param mdataArea 条件
     * @return List<MdataAreaDo>
     */
    @Override
    public List<MdataAreaDo> selectByExample(MdataAreaQuery mdataArea) {
        return getMapper().selectByExample(buildExample(mdataArea));
    }

    /**
     * 新增地区信息
     *
     * @param mdataArea 条件
     * @return int>
     */
    @Override
    public int insertBySelective(MdataAreaDo mdataArea) {
        if (mdataArea == null) {
            return -1;
        }
        mdataArea.setCreateTime(new Date());
        mdataArea.setUpdateTime(new Date());
        return getMapper().insertSelective(mdataArea);
    }

    /**
     * 修改地区信息
     *
     * @param mdataArea
     * @return
     */
    @Override
    public int updateBySelective(MdataAreaDo mdataArea) {
        if (mdataArea == null) {
            return -1;
        }
        mdataArea.setUpdateTime(new Date());
        return getMapper().updateByPrimaryKeySelective(mdataArea);
    }

    /**
     * 清理行政区划数据
     */
    @Override
    public void cleanData() {
        getMapper().deleteByExample(buildExample(MdataAreaQuery.builder()
            .build()));
    }

    /**
     * 查询所有省，直辖市
     * @return
     */
    @Override
    public List<MdataAreaDo> queryAllProvinceCode() {
        return getMapper().queryAllProvinceCode();
    }

    /**
     * 根据省份编码查询所有市编码
     *
     * @param provinceCode
     * @return
     */
    @Override
    public List<MdataAreaDo> queryAllCityCodeByProvinceCode(String provinceCode) {

        return getMapper().queryAllCityCodeByProvinceCode(provinceCode);
    }

    /**
     * 根据市编码查询所有区，县编码
     *
     * @param cityCode
     * @return
     */
    @Override
    public List<MdataAreaDo> queryAllAreaCodeByCityCode(String cityCode) {
        return getMapper().queryAllAreaCodeByCityCode(cityCode);
    }

    /**
     * 构建地区Example信息
     *
     * @param mdataArea
     * @return
     */
    public MdataAreaExample buildExample(MdataAreaQuery mdataArea) {
        MdataAreaExample example = new MdataAreaExample();
        MdataAreaExample.Criteria criteria = example.createCriteria();
        if (mdataArea != null) {
            //添加查询条件
            if (StringUtil.isNotEmpty(mdataArea.getAreaCode())) {
                criteria.andAreaCodeEqualTo(mdataArea.getAreaCode());
            }
            if (StringUtil.isNotEmpty(mdataArea.getAreaId())) {
                criteria.andAreaIdEqualTo(mdataArea.getAreaId());
            }
            if (StringUtil.isNotEmpty(mdataArea.getAreaName())) {
                criteria.andAreaNameEqualTo(mdataArea.getAreaName());
            }
            if (StringUtil.isNotEmpty(mdataArea.getCityCode())) {
                criteria.andCityCodeEqualTo(mdataArea.getCityCode());
            }
            if (StringUtil.isNotEmpty(mdataArea.getCityId())) {
                criteria.andCityIdEqualTo(mdataArea.getCityId());
            }
            if (StringUtil.isNotEmpty(mdataArea.getCityName())) {
                criteria.andCityNameEqualTo(mdataArea.getCityName());
            }
            if (StringUtil.isNotEmpty(mdataArea.getProvinceCode())) {
                criteria.andProvinceCodeEqualTo(mdataArea.getProvinceCode());
            }
            if (StringUtil.isNotEmpty(mdataArea.getProvinceName())) {
                criteria.andProvinceNameEqualTo(mdataArea.getProvinceName());
            }
        }
        //添加排序字段
        example.setOrderByClause(" create_time desc");
        return example;
    }
}
